import { defineStore } from 'pinia'
import { ref } from 'vue'
import {loginApi} from '@/api/login'

export const useUserStore = defineStore(
  'charlie-user',
  () => {
    const userInfo = ref()
    const token = ref()

    const setToken = (data: any) => {
      token.value = data
    }
    const delToken = ()=>{
      token.value = ''
      localStorage.setItem('token', '')
    }

    const setUserInfo = (data: any) => {
      userInfo.value = data
    }
    const delUserInfo = () => {
      userInfo.value = ''
    }

    const login = async(data:any)=>{
      const res = await loginApi(data)
      setToken(res.access_token)
    }

    return { userInfo, setUserInfo, delUserInfo, token, setToken, delToken , login}
  },
  {
    // 本地仓库持久化存储
    persist: true,
  }
)
